//
//  UIViewController+HDPingJiaViewController.m
//  app
//
//  Created by apple on 2017/7/19.
//  Copyright © 2017年 hdaren. All rights reserved.
//

#import "HDPingJiaViewController.h"
#import "HDRepairDetailManager.h"
#import "HDPIngJiaCell.h"

@interface HDPingJiaViewController ()<UITextViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UILabel * placeholderLabel;
@property (nonatomic,strong) HDRepairDetailManager * manager;
@property (nonatomic,strong) HDPingJiaResult * request;
@property (nonatomic,strong) MBProgressHUD* progress;
@property (nonatomic,strong) UITextView * opinionView;
@property (nonatomic,strong) UITableView * tableview;

@end

@implementation  HDPingJiaViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"提交评价";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.opinionView = [[UITextView alloc]initWithFrame:CGRectOf([self.view.ubo fx:0 ox:12 fy:0 oy:28+64], [self.view.ubo ow:UBO_SCREEN.width-24 oh:150])];
    self.opinionView.delegate = self;
    [self.opinionView setFont:[UIFont systemFontOfSize:15]];
    [self.opinionView setTextColor:[XCommon hexStringToColor:kColor_l_light_gray_text]];
    self.opinionView.backgroundColor = [XCommon hexStringToColor:kColor_orderback_gray];
    self.opinionView.layer.masksToBounds = YES;
    self.opinionView.layer.cornerRadius = UF(6);
    self.opinionView.layer.borderWidth = 1;
    self.opinionView.layer.borderColor = [XCommon hexStringToColor:kColor_m_light_gray_background].CGColor;
    [self.view addSubview:self.opinionView];
    
    self.placeholderLabel = [[UILabel alloc]initWithFrame:CGRectOf([self.opinionView.ubo fx:0 ox:6 fy:0 oy:12 ], [self.opinionView.ubo ow:UBO_SCREEN.width-36 oh:12])];
    self.placeholderLabel.text = @"亲，您的评价对其他用户有很大的帮助";
    [self.placeholderLabel setFont:[UIFont systemFontOfSize:12]];
    [self.placeholderLabel setTextColor:[XCommon hexStringToColor:kColor_s_light_gray_text]];
    [self.placeholderLabel setEnabled:NO];
    [self.opinionView addSubview:self.placeholderLabel];
    
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectOf([self.opinionView.ufo fx:UB_1_2 ox:-(UBO_SCREEN.width-48)/2 fy:1 oy:28], [self.view .ubo ow:UBO_SCREEN.width-48 oh:40])];
    btn.backgroundColor = [XCommon hexStringToColor:kColor_red_text];
    [btn setTitle:@"提交评价" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.layer.masksToBounds = YES;
    btn.layer.cornerRadius = UF(6);
    [btn addTarget:self action:@selector(opinion:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];

    self.manager = [HDRepairDetailManager shareManager];
    self.request = [[HDPingJiaResult alloc]initWithTarget:self action:@selector(ipinionRequest:)];
    
    
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectOf([btn.ufo fx:UB_1_2 ox:-UBO_SCREEN.width/2 fy:1 oy:12], [self.view.ubo ow:UBO_SCREEN.width oh:UBO_SCREEN.height -64-28-28-150-40-12]) style:UITableViewStyleGrouped];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableview];
    
    
    
    
    
    
}


-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}



#pragma mark - tableView代理
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.pingjia_array.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentifier";
    HDPIngJiaCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[HDPIngJiaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier ];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
//    cell.delegate = self;
    
    NSDictionary * dic = [self.pingjia_array objectAtIndex:[indexPath section]];
    [cell cellWithModel:dic];
    
    
    cell.backgroundView = nil;
    
    return cell;
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80.0f;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 20.0f;
    }else
    {
        return 0.01f;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (self.pingjia_array.count > 0) {
        
        if (section == 0) {
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UBO_SCREEN.width, 20)];
            
            UILabel * lab = [[UILabel alloc]initWithFrame:CGRectOf([view.ubo fx:0 ox:12 fy:UB_1_2 oy:-14/2], [view.ubo ow:UBO_SCREEN.width-24 oh:14])];
            lab.text = @"历史评价";
            [lab setTextColor:[UIColor redColor]];
            [lab setFont:[UIFont systemFontOfSize:14]];
            [view addSubview:lab];
            
            return view;
        }else
        {
            return nil;
        }
        
        
        
        
        
    }else
    {
        return nil;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}




-(void)opinion:(UIButton*)btn
{
    
    if (self.opinionView.text.length > 0) {
        
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        
//        NSDictionary * params = @{@"evaluation":self.opinionView.text,
//                                  @"repair_apply_id":self.repair_apply_id,
//                                  @"operator":self.operator_user};
        
        NSDictionary * params = [NSDictionary dictionaryWithObjectsAndKeys:self.opinionView.text,@"evaluation",self.repair_apply_id,@"repair_apply_id",self.operator_user,@"operator", nil];
        
        [self.manager submitPingjiaData:self.request Params:params];
        
    }else
    {
        [MyToast showWithText:@"请填写您的宝贵意见"];
    }
    
    
    
    
}


-(void)ipinionRequest:(HDPingJiaResult*)rt
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    [MyToast showWithText:rt.message];
    
    if (rt.success) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }else
    {
    
    }
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

{    if (![text isEqualToString:@""])
    
{
    
    self.placeholderLabel.hidden = YES;
    
}
    
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
        
    {
        
        self.placeholderLabel.hidden = NO;
        
    }
    
    return YES;
    
}







@end
